/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.emoji.EmojiUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.param.UserRegisterParam;
import com.yami.shop.bean.vo.UserVO;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.dao.UserMapper;
import com.yami.shop.service.CouponRecordService;
import com.yami.shop.service.UserScoreRecordService;
import com.yami.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

import static org.checkerframework.checker.units.qual.Prefix.one;

/**
 * @author lgh on 2018/09/11.
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserScoreRecordService userScoreRecordService;
    @Autowired
    private CouponRecordService couponRecordService;


    @Override
    @Cacheable(cacheNames = "user", key = "#userId")
    public User getUserByUserId(String userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public Boolean insertUser(UserRegisterParam uParam) {
        User mail = userMapper.getUserByUserMail(uParam.getUserMail());
        if (mail != null) {
            throw new YamiShopBindException("账号已存在");
        }
        Date now = new Date();
        User user = new User();
        user.setUserId(IdUtil.simpleUUID());
        user.setModifyTime(now);
        user.setUserRegtime(now);
        user.setStatus(1);
        user.setNickName(uParam.getNickName());
        user.setUserMail(uParam.getUserMail());
        user.setLoginPassword(uParam.getPassword());

        user.setUserCode(uParam.getUserCode());
        user.setScore(0.0);
        user.setCouponCount(0);
        return userMapper.insert(user) == 1;
    }

    @Override
    public void addScore(String userCode, double score,String mark) {
        User one = getByUserCode(userCode);
        if (one == null) {
            UserRegisterParam userRegisterParam = new UserRegisterParam();
            userRegisterParam.setUserCode(userCode);
            userRegisterParam.setUserMail(RandomUtil.randomNumbers(10) + "@123.com");
            userRegisterParam.setPassword("123");
            userRegisterParam.setNickName(userCode);
            insertUser(userRegisterParam);
            one = getByUserCode(userCode);
        }
        addScoreByUid(one.getUserId(),score,mark);
    }

    @Override
    public void subScore(String userCode, double score,String mark) {
        User one = getUserByUserId(userCode);
        subScoreByUid(one.getUserId(),score,mark);
    }

    @Override
    public void addScoreByUid(String userId, double score,String mark) {
        User one = getById(userId);
        Double oldScore = one.getScore();
        one.setScore(oldScore + score);
        updateById(one);

        //更新记录
        userScoreRecordService.addScoreRecord(one.getUserId(), score, 2, mark);
    }

    @Override
    public void subScoreByUid(String userId, double score,String mark) {
        User one = getById(userId);
        if (one == null) {
            throw new RuntimeException("无此用户");
        }
        double newScore;
        newScore = one.getScore() - score;
        if (newScore < 0) {
            throw new YamiShopBindException("积分不足");
        }
        one.setScore(newScore);
        updateById(one);

        //更新记录
        userScoreRecordService.addScoreRecord(one.getUserId(), score, 1, mark);
    }

    @Override
    public void addCoupon(String userCode, Integer count,String mark) {
        User one = getByUserCode(userCode);
        if (one == null) {
            UserRegisterParam userRegisterParam = new UserRegisterParam();
            userRegisterParam.setUserCode(userCode);
            userRegisterParam.setUserMail(RandomUtil.randomNumbers(10) + "@123.com");
            userRegisterParam.setPassword("123");
            userRegisterParam.setNickName(userCode);
            insertUser(userRegisterParam);
            one = getByUserCode(userCode);
        }
        addCouponByUid(one.getUserId(), count,mark);
    }

    @Override
    public void addCouponByUid(String uid, Integer count,String mark) {
        User one = getById(uid);
        Integer old = one.getCouponCount();
        one.setCouponCount(old + count);
        updateById(one);
        couponRecordService.addCouponRecordByUid(uid, count, 1,mark);
    }

    @Override
    public void subCouponByUid(String uid, Integer count, String mark) {
        User one = getById(uid);
        Integer old = one.getCouponCount();
        int newCount = old - count;
        if(newCount < 0){
            throw new RuntimeException("订单扣除优惠券时出错");
        }
        one.setCouponCount(newCount);
        updateById(one);

        couponRecordService.addCouponRecordByUid(uid, count, 2,mark);

    }

    @Override
    public User getByUserCode(String userCode) {
        User one = getOne(Wrappers.lambdaQuery(new User())
                .eq(User::getUserCode, userCode));
        return one;
    }

}
